//-----------------------------------------------------------------------------
//  File          : coremain.c
//  Module        :
//  Description   : Main Entry.
//  Author        : Auto Generate by coremain_c.bat
//  Notes         :
//
// ============================================================================
// | Naming conventions                                                       |
// | ~~~~~~~~~~~~~~~~~~                                                       |
// |         Struct/Array : Leading s                                         |
// |             Constant : Leading K                                         |
// |      Global Variable : Leading g  (variable use outside one C file)      |
// |                                                                          |
// |   BYTE( 8 Bits) size : Leading b                                         |
// |   CHAR( 8 Bits) size : Leading c                                         |
// |   WORD(16 Bits) size : Leading w                                         |
// |  DWORD(32 Bits) size : Leading d                                         |
// |    int(32 Bits) size : Leading i                                         |
// | DDWORD(64 Bits) size : Leading dd                                        |
// |              Pointer : Leading p                                         |
// |                                                                          |
// |    Function argument : Leading a                                         |
// |       Local Variable : All lower case                                    |
// |                                                                          |
// | Examples:                                                                |
// |                Array : Leading s, (sb = arrary of byte, etc)             |
// |              Pointer : Leading p, (pb = pointer of byte, etc)            |
// ============================================================================
//
//  Date          Who         Action
//  ------------  ----------- -------------------------------------------------
//  DD MMM  YYYY  coremain_c  Initial Version.
//  01 Apr  2009  Lewis       Add Keydll stuff.
//-----------------------------------------------------------------------------
#include <stdio.h>
#include "system.h"
#include "key2dll.h"
#include "kbdutil.h"
#include "secufile.h"
#include "oper.h"
#include "keycmd.h"
#include "dukpt.h"
#include "sysutil.h"
#include "coremain.h"
#include "midware.h"
#include "apm.h"
//-----------------------------------------------------------------------------
//      Prototypes
//-----------------------------------------------------------------------------
extern void Authentication(const char *aApplVer, const char *aKey, DWORD aMode);

//-----------------------------------------------------------------------------
//      Defines
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
//      Globals
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
//      Constant
//-----------------------------------------------------------------------------
const BYTE KSoftName[] = {"KEYDLL"};


BOOLEAN CreatTMSFile(void);
BOOLEAN CheckTMSFile(void);
void DeleteTMSFile(void);

//-----------------------------------------------------------------------------
//      Export Functions
//-----------------------------------------------------------------------------
void XF_ResetKeyBank(int x)                    { ResetKeyBank(x); os_appcall_return(0); }
void XF_KeyStatus(BYTE *x)                     { KeyStatus(x); os_appcall_return(0); }
void XF_KeyInject(BYTE *x)                     { os_appcall_return(KeyInject(x)); }
void XF_KeyEncrypt(BYTE x, BYTE *y, WORD z)    { os_appcall_return(KeyEncrypt(x, y, z)); }
void XF_KeyDecrypt(BYTE x, BYTE *y, WORD z)    { os_appcall_return(KeyDecrypt(x, y, z)); }
void XF_KeyTriEncrypt(BYTE x, BYTE *y, WORD z) { os_appcall_return(KeyTriEncrypt(x, y, z)); }
void XF_KeyTriDecrypt(BYTE x, BYTE *y, WORD z) { os_appcall_return(KeyTriDecrypt(x, y, z)); }
void XF_GetPinX98(BYTE *x, BYTE *y, BYTE z)    { os_appcall_return(GetPinX98(x, y, z)); }
void XF_X99CalMac(BYTE *x, BYTE *y, DWORD z)   { os_appcall_return(X99CalMac(x, y, z)); }
void XF_KeyCalMac(BYTE x, BYTE *y, WORD z)    {os_appcall_return(KeyCalMac(x, y, z));}
void XF_SetOpMode(BYTE x, BYTE *y)             { SetOpMode(x, y); os_appcall_return(0); }
void XF_GetOpMode(BYTE *x)                     { GetOpMode(x); os_appcall_return(0); }
void XF_DukptKeyReset(BYTE x)                  { os_appcall_return(DukptKeyReset(x)); }
void XF_DukptKeyInject(BYTE *x)                { os_appcall_return(DukptKeyInject(x)); }
void XF_DukptGetPin(BYTE *x, BYTE *y, BYTE z)  { os_appcall_return(DukptGetPin(x,y,z)); }
void XF_ExtPPadSetup(DWORD x, DWORD y)         { ExtPPadSetup(x, y); os_appcall_return(0);}
void XF_InitOper(void)                         {OperFileInit(); os_appcall_return(0);}
void XF_GetOperPsw(WORD x,WORD y)              {os_appcall_return(GetOperPsw(x,y));}
void XF_TransOperPsw(WORD x,WORD y,BYTE *z)    {os_appcall_return(TransOperPsw(x,y,z));}
void XF_AddOper(WORD x,WORD y,WORD z)          {os_appcall_return(AddOper(x,y,z));}
void XF_DeleteOper(WORD x,WORD y)              {os_appcall_return(DeleteOper(x,y));}
void XF_ModifyOperPsw(WORD x)                  {os_appcall_return(ModifyOperPsw(x));}
void XF_UpdataOperState(WORD x,WORD y)         {os_appcall_return(UpdataOperState(x,y));}
void XF_GetOperState(WORD x)                   {os_appcall_return(GetOperState(x));}
void XF_GetOperType(WORD x)                    {os_appcall_return(GetOperType(x));}
void XF_DispAllOp()                              {DispAllOp();os_appcall_return(0);}

//tms+ zx 110315
void XF_CreatTMSFile(void)                           { os_appcall_return(CreatTMSFile()); }
void XF_CheckTMSFile(void)                           { os_appcall_return(CheckTMSFile()); }
void XF_DeleteTMSFile(void)                          { DeleteTMSFile(); os_appcall_return(0); }
//tms+ end

const DWORD KExportFunc[] =    // Export Function Table
{
  (DWORD) KDLL_FUNC_COUNT           , // Importance!! Number of export functions!!!
  (DWORD) XF_ResetKeyBank           ,
  (DWORD) XF_KeyStatus              ,
  (DWORD) XF_KeyInject              ,
  (DWORD) XF_KeyEncrypt             ,
  (DWORD) XF_KeyDecrypt             ,
  (DWORD) XF_KeyTriEncrypt          ,
  (DWORD) XF_KeyTriDecrypt          ,
  (DWORD) XF_GetPinX98              ,
  (DWORD) XF_X99CalMac              ,
  (DWORD) XF_KeyCalMac              ,
  (DWORD) XF_SetOpMode              ,
  (DWORD) XF_GetOpMode              ,
  (DWORD) XF_DukptKeyReset          ,
  (DWORD) XF_DukptKeyInject         ,
  (DWORD) XF_DukptGetPin            ,
  (DWORD) XF_ExtPPadSetup           ,
  (DWORD) XF_InitOper               ,
  (DWORD) XF_GetOperPsw             ,
	(DWORD) XF_TransOperPsw           ,
  (DWORD) XF_AddOper                ,
  (DWORD) XF_DeleteOper             ,
  (DWORD) XF_ModifyOperPsw          ,
  (DWORD) XF_UpdataOperState        ,
  (DWORD) XF_GetOperState           ,
  (DWORD) XF_GetOperType            ,
  (DWORD) XF_DispAllOp              ,
  //tms+ zx 110315
  (DWORD) XF_CreatTMSFile           ,
  (DWORD) XF_CheckTMSFile           ,
  (DWORD) XF_DeleteTMSFile          ,
//tms+ end
};


//tms+ zx 110315 start
const char KAppDLFile[] = {"AppDLFile"};

//*****************************************************************************
//  Function        : CreatTMSFile
//  Description     : Creat TMS File
//  Input           : N/A
//  Return          : T/F
//  Note            : N/A
//  Globals Changed : N/A
//*****************************************************************************
BOOLEAN CreatTMSFile(void)
{
  BYTE filename[32];
  int hTMSFile = -1;

  memset(filename, 0, 32);
  strcpy(filename, KAppDLFile);
  os_file_delete(filename);
  hTMSFile = os_file_open(filename, K_O_RDWR | K_O_TRUNC | K_O_CREAT);
  if (hTMSFile < 0)
  {
	DispLine("Create TMS ERR",SU_LINE3, SU_CLRDISP|SU_CENTER|SU_BIGFONT);
    APM_WaitKey(9000, 0);
    return 0;
  }
  os_file_close(hTMSFile);
  fCommitAllMW();
  return 1;
}

//*****************************************************************************
//  Function        : CheckTMSdown
//  Description     : Check TMS Down or Local Down
//  Input           : N/A
//  Return          : T/F
//  Note            : N/A
//  Globals Changed : N/A
//*****************************************************************************
BOOLEAN CheckTMSFile(void)
{
  BYTE filename[32];

  memset(filename, 0, 32);
  strcpy(filename, KAppDLFile);

  if (!os_file_exist(filename))
    return 0;

  return 1;
}

//*****************************************************************************
//  Function        : DeleteTMSFile
//  Description     : Delete TMS File
//  Input           : N/A
//  Return          : N/A
//  Note            : N/A
//  Globals Changed : N/A
//*****************************************************************************
void DeleteTMSFile(void)
{
  BYTE filename[32];

  memset(filename, 0, 32);
  strcpy(filename, KAppDLFile);
  os_file_delete(filename);
}
//tms+ end

//*****************************************************************************
//  Function        : VirginInit
//  Description     : Perform virgin initialization.
//  Input           : N/A
//  Return          : N/A
//  Note            : N/A
//  Globals Changed : N/A
//*****************************************************************************
int VirginInit(void)
{
  DispLine("KEYDLL INIT.", SU_LINE3, SU_CLRDISP|SU_CENTER|SU_BIGFONT);
  DispLine("PLEASE WAIT...", SU_LINE5, SU_CENTER|SU_BIGFONT);

  if (CheckTMSFile())//tms+ zx 110315
    return 0;

  ClrAllFiles();
  return 0;
}
//*****************************************************************************
//  Function        : GlobalsInit
//  Description     : Initialization global variables.
//  Input           : N/A
//  Return          : N/A
//  Note            : N/A
//  Globals Changed : N/A
//*****************************************************************************
void GlobalsInit(void)
{
  BYTE temper;

  // re-init the DLL if tamper.
  if (os_config_read(K_CF_Tampered, &temper)) {
    if (temper != 0xFF) {
      VirginInit();
      temper = 0xFF;
      os_config_write(K_CF_Tampered, &temper);
      os_config_update();
    }
  }
  #ifndef WIN32
 //   Authentication(KSoftName, NULL, K_AuthSecurity);
  #endif
  SecuFileInit();
  OperFileInit();
}
//*****************************************************************************
//  Function        : GlobalsCleanUp
//  Description     : Clean Up all allocated globals.
//  Input           : N/A
//  Return          : N/A
//  Note            : N/A
//  Globals Changed : N/A
//*****************************************************************************
void GlobalsCleanUp(void)
{
  // !TTDo Nothing
}
//*****************************************************************************
//  Function        : Coremain
//  Description     : Your application code start here.
//  Input           : N/A
//  Return          : N/A
//  Note            : N/A
//  Globals Changed : N/A
//*****************************************************************************
int Coremain(DWORD aParam1, DWORD aParam2, DWORD aParam3)
{
  return 0;
}
